// newHousePackage/pages/agentEnterHouseApply/agentEnterHouseApply.js

import { chooseAndUploadQiniuActicon } from "../../../../service/upload_service"
import { agentApplyHouseExpert } from "../../../../service/platform_service"
import debounce from "../../../../tools/debounce"
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    formData: {
      name: "",
      phone: "",
      wxChat: "",
      ewmPic: "",
      houseNumber: "",
    },
    showRemindPop: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      'formData.houseNumber': options.houseNumber,
      'formData.name': app.globalData.name || "",
      'formData.phone': app.globalData.phone || "",
      'formData.wxChat': app.globalData.weChat || "",
    })
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
    if (app.globalData.wxCodeImg) {
      this.setData({
        'formData.ewmPic': app.globalData.wxCodeImg
      })
      app.globalData.wxCodeImg = "";
    }
  },

  //输入绑定
  inputCellChange(e) {
    let item = e.detail;
    let changeValue = `formData.${item.key}`
    this.setData({
      [changeValue]: item.value
    })

  },
  //上传单独那一张身份证明:
  pushProverImgAction() {
    wx.navigateTo({
      url: '/formPackage/pages/platformModule/cropperImg/cropperImg?imgSrc=' + "",
    })
    return;
    chooseAndUploadQiniuActicon('ew').then(res => {
      console.log("JJLog:图片上传成功:" + JSON.stringify(res));
      wx.showToast({
        title: '上传成功',
      })
      this.setData({
        'formData.ewmPic': res.complateUrl
      })

    }).catch(err => {
      console.log("JJLog:图片上传失败:" + JSON.stringify(err));
      wx.showToast({
        title: '图片上传失败',
        icon: "none"
      })
    })
  },


  //确定入驻
  submitHouseAction() {
    let data = this.data.formData;
    if (!data.name) {
      wx.showToast({
        title: '请输入您的真实姓名',
        icon: "none"
      })
      return;
    }
    if (!data.phone || data.phone.length != 11) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: "none"
      })
      return;
    }
    if (!data.wxChat) {
      wx.showToast({
        title: '请输入您的微信号',
        icon: "none"
      })
      return;
    }

    if (!data.ewmPic) {
      wx.showToast({
        title: '请上传您的微信二维码',
        icon: "none"
      })
      return;
    }
    console.log("JJLog:提交数据是:" + JSON.stringify(this.data.formData));

    debounce(this.agentApplyHouseExpert)
  },

  agentApplyHouseExpert() {
    wx.showLoading();
    agentApplyHouseExpert(this.data.formData).then(res => {
      wx.hideLoading();
      console.log("JJLog:搜索结果:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          showRemindPop: true
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "入驻失败",
        icon: "none"
      })
      console.error("JJLog:agentApplyHouseExpert", err);
    })
  },

  sureRemindPopAction() {
    this.setData({
      showRemindPop: false
    })
    wx.navigateBack({
      delta: 3,
    })
  },

})